package com.ruoyi.industry.controller.back;

import java.util.ArrayList;
import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.security.annotation.Logical;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.industry.domain.SpaceIndustry;
import com.ruoyi.industry.service.ISpaceIndustryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.industry.domain.ProjectApartment;
import com.ruoyi.industry.service.IProjectApartmentService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 公寓Controller
 *
 * @author MagicCube
 * @date 2023-05-08
 */
@RestController
@RequestMapping("/apartment")
public class ProjectApartmentController extends BaseController
{
    @Autowired
    private IProjectApartmentService projectApartmentService;

    @Autowired
    private ISpaceIndustryService spaceIndustryService;
    /**
     * 查询公寓列表
     */
    @RequiresPermissions("industry:apartment:list")
    @GetMapping("/list")
    public TableDataInfo list(ProjectApartment projectApartment)
    {
        startPage();
        List<ProjectApartment> list = projectApartmentService.selectProjectApartmentList(projectApartment);
        return getDataTable(list);
    }

    /**
     * 导出公寓列表
     */
    @RequiresPermissions("industry:apartment:export")
    @Log(title = "公寓", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ProjectApartment projectApartment)
    {
        List<ProjectApartment> list = projectApartmentService.selectProjectApartmentList(projectApartment);
        ExcelUtil<ProjectApartment> util = new ExcelUtil<ProjectApartment>(ProjectApartment.class);
        util.exportExcel(response, list, "公寓数据");
    }

    /**
     * 导出免费服务委托书
     */
    @RequiresPermissions("industry:apartment:export")
    @Log(title = "公寓免费服务委托书", businessType = BusinessType.EXPORT)
    @GetMapping("/entrust")
    public void entrust(HttpServletResponse response, ProjectApartment projectApartment)
    {
//        ProjectApartment apartment = projectApartmentService.selectProjectApartmentById(projectApartment.getId());
        projectApartmentService.exportEntrust(response,projectApartment.getId());
    }

    /**
     * 下载模板
     * @param response response
     * @throws IOException
     */
//    @RequiresPermissions("industry:apartment:export")
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) throws IOException{
        List<ProjectApartment> list = new ArrayList<>();
        ProjectApartment apartment = ProjectApartment.getProjectApartment();
        list.add(apartment);
        ExcelUtil<ProjectApartment> util = new ExcelUtil<>(ProjectApartment.class);
        util.exportExcel(response, list, "公寓数据");
//        util.importTemplateExcel(response,"公寓管理",list);
    }

    /**
     * 数据导入
     * @param file 文件
     * @param updateSupport 更新支持
     * @return
     * @throws Exception
     */
    @Log(title = "公寓管理", businessType = BusinessType.IMPORT)
    @RequiresPermissions("industry:apartment:import")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<ProjectApartment> util = new ExcelUtil<>(ProjectApartment.class);
        List<ProjectApartment> projectApartmentList = util.importExcel(file.getInputStream());
        String operName = SecurityUtils.getUsername();
        String message = projectApartmentService.importUser(projectApartmentList, updateSupport, operName);
        return success(message);
    }

    /**
     * 获取公寓详细信息
     */
    @RequiresPermissions("industry:apartment:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(projectApartmentService.selectProjectApartmentById(id));
    }

    /**
     * 新增公寓
     */
    @RequiresPermissions("industry:apartment:add")
    @Log(title = "公寓", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ProjectApartment projectApartment)
    {
        return toAjax(projectApartmentService.insertProjectApartment(projectApartment));
    }

    /**
     * 修改公寓
     */
    @RequiresPermissions("industry:apartment:edit")
    @Log(title = "公寓", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ProjectApartment projectApartment)
    {
        return toAjax(projectApartmentService.updateProjectApartment(projectApartment));
    }

    /**
     * 删除公寓
     */
    @RequiresPermissions("industry:apartment:remove")
    @Log(title = "公寓", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(projectApartmentService.deleteProjectApartmentByIds(ids));
    }
}
